package com.example.liwushuo;

import com.example.liwushuo.utils.Utils;
import com.lidroid.xutils.view.annotation.ViewInject;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.TextView;

public class SelectActivity extends Activity implements OnClickListener {

	private ImageView ImgMan;
	private ImageView ImgWoman;
	private Intent intent;

	private String gender;
	private String generation;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		// 设置弹出对话框的时候背景不变暗
		setTheme(R.style.dialog);

		ImageView img = new ImageView(getApplicationContext());
		img.setImageResource(R.drawable.bg_user_select_type);
		img.setScaleType(ScaleType.CENTER_INSIDE);
		setContentView(img);
		showDialogMethod();

		intent = new Intent(SelectActivity.this, MainActivity.class);
	}

	private AlertDialog selectDialog; // 注册对话框

	// 显示自定义的弹出对话框
	// 此处的上下文必须是当前的Activity对象 自定义对话框
	private void showDialogMethod() {
		if (selectDialog == null) {
			MyViewMethod();
		}
		selectDialog.show();

	}

	private void MyViewMethod() {

		View view = getLayoutInflater().inflate(R.layout.mydialog, null);
		ImgMan = (ImageView) view.findViewById(R.id.img_man_id);
		ImgWoman = (ImageView) view.findViewById(R.id.img_woman_id);
		ImgMan.setOnClickListener(this);
		ImgWoman.setOnClickListener(this);

		selectDialog = new AlertDialog.Builder(this).setTitle("请选择您的性别")
				.setView(view) // 将自定义的view添加到弹出对话框中
				.setCancelable(false)// 点击窗口以外的地方不消失
				.create();
	}

	private AlertDialog RankDialog; // 身份对话框
	private TextView chuzhong;
	private TextView gaozhong;
	private TextView daxue;
	private TextView zhichang;
	private TextView zishen;

	private void showRankDialog() {

		if (RankDialog == null) {
			MyRonkViewMethod();
		}
		RankDialog.show();
	}

	private void MyRonkViewMethod() {

		View view = getLayoutInflater().inflate(R.layout.myronkdialog, null);
		chuzhong = (TextView) view.findViewById(R.id.tv_chuzhong);
		gaozhong = (TextView) view.findViewById(R.id.tv_gaozhong);
		daxue = (TextView) view.findViewById(R.id.tv_daxue);
		zhichang = (TextView) view.findViewById(R.id.tv_zhichang);
		zishen = (TextView) view.findViewById(R.id.tv_zishen);

		RankDialog = new AlertDialog.Builder(this).setTitle("请选择您的身份")
				.setView(view) // 将自定义的view添加到弹出对话框中
				.setCancelable(false)// 点击窗口以外的地方不消失
				.create();

		chuzhong.setOnClickListener(this);
		gaozhong.setOnClickListener(this);
		daxue.setOnClickListener(this);
		zhichang.setOnClickListener(this);
		zishen.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		// 跳转到选择身份的界面

		switch (v.getId()) {
		case R.id.img_man_id:
			gender = "1";
			break;
		case R.id.img_woman_id:
			gender = "2";
			break;
		case R.id.tv_chuzhong:
			generation = "0";
			break;
		case R.id.tv_gaozhong:
			generation = "1";
			break;
		case R.id.tv_daxue:
			generation = "2";
			break;
		case R.id.tv_zhichang:
			generation = "3";
			break;
		case R.id.tv_zishen:
			generation = "4";
			break;

		}
		if (RankDialog != null) {
			// intent.putExtra("generation", generation);
			Utils.SaveParams(getApplicationContext(), "generation", generation);
			startActivity(intent);
			finish();
		}

		if (selectDialog.isShowing()) {
			// intent.putExtra("gender", gender);
			Utils.SaveParams(getApplicationContext(), "gender", gender);
			selectDialog.dismiss(); // 隐藏对话框
			showRankDialog();
		}
	}
}
